<?php

namespace App\Extensions\Actions\Post;

use App\BookApply;
use App\Extensions\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class BookGrantPost extends RowAction
{
    public $name = '请上传著作权证书';

    public function handle(BookApply $model, Request $request)
    {
        $user = Admin::user();
        if ($user['role_type'] == 1) {
            $model->status = 1;
        }
        $model->name = $request->get('name');
        if ($register_no = $request->get('register_no')) {
            $model->register_no = $register_no;
        }
        if ($complete_date = $request->get('complete_date')) {
            $model->complete_date = $complete_date;
        }
        if ($cert_date = $request->get('cert_date')) {
            $model->cert_date = $cert_date;
        }
        if ($file = $request->file('cert')) {
            $model->storeFile($file,'cert');
        } elseif (!$model->cert) {
            $this->response()->status = false;
            return $this->response()->swal()->error('请上传证书');
        }
        $model->save();

        return $this->response()->swal()->success('操作成功')->refresh();
    }

    public function form()
    {
        $model = $this->row;
        $this->text('name', __('名称'))->default($model->name);
        $this->text('register_no', __('登记号'))
            ->default($model->register_no)
            ->rules('required', ['required' => '请填写登记号']);
        $this->date('complete_date', __('完成日期'))
            ->default($model->complete_date)
            ->rules('required', ['required' => '请填写完成日期']);
        $this->date('cert_date', __('下证时间'))
            ->default($model->cert_date)
            ->rules('required', ['required' => '请填写下证时间']);
        $this->file('cert', '上传证书');
    }


}
